<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"  xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="_header_include::frag(~{::title},~{},~{})">
    <title>后台管理系统</title>
</head>
<body>
<div id="maindiv" class="maindiv mainPadding" style="display: none;">    
    <el-breadcrumb separator="/" class="elBreadcrumb marginB20">
        <el-breadcrumb-item>Home</el-breadcrumb-item>
        <el-breadcrumb-item>数据报表</el-breadcrumb-item>
        <el-breadcrumb-item>分配资源跟访时间分布表</el-breadcrumb-item>
    </el-breadcrumb>    
    <el-row class="marginB20">
        <el-button type="primary" @click="searchYesterday">昨天</el-button>
        <el-button type="success" @click="searchWeek">近七天</el-button>
        <el-button type="warning" @click="searchMonth">本月</el-button>
        <!-- <shiro:hasPermission name="statistics:businessVisitReceptionTable:export"> -->
        <el-button type="danger" @click="exportRs">导出Excel报表</el-button>
        <!-- </shiro:hasPermission> -->
    </el-row>
    <div class="mainSearchBg">        
        <div class="mainSearchFormBox">
           <el-form :inline="true" :model="form" class="demo-form-inline mainSearchForm" ref="form">
                <el-form-item label="统计日期：">
                    <el-date-picker format="yyyy-MM-dd HH:mm" v-model="form.startTime" type="datetime" value-format="yyyy-MM-dd HH:mm" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="—" class="widthauto">
                    <el-date-picker format="yyyy-MM-dd HH:mm" v-model="form.endTime" type="datetime" value-format="yyyy-MM-dd HH:mm" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="电销事业部：">  
                    <el-select v-model="form.teleDeptId" placeholder="全部" style="width: 100%;" clearable filterable @change="curTelGroupSale" :disabled="isDisabled">
                        <el-option
                            v-for="item in deptList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="电销组：">  
                    <el-select v-model="form.teleGroupId" placeholder="全部" style="width: 100%;" clearable filterable  :disabled="isDisabledOrgId">
                        <el-option
                            v-for="item in orgTeleArr"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="分配来源：">  
                    <el-select v-model="form.source" placeholder="全部" style="width: 100%;" clearable filterable>
                        <el-option
                            v-for="item in sourceList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <div class="mainSearchBtnBox">
                    <el-button icon="el-icon-search" type="primary" @click="searchTable(1)" class="searchBtn" :disabled="loading">搜索</el-button>
                </div>  
            </el-form>
        </div>
        <el-row>
            <el-table 
                ref="tableData"
                tooltip-effect="dark"
                style="width: 100%"
                border
                :data="tableData"
                v-loading="loading"
                :cell-style="centerStyle"
                :header-cell-style="centerStyle"
                :span-method="arraySpanMethod">
                <el-table-column type="index" label="序号" width="55" :index="indexFun"></el-table-column>
                <el-table-column prop="teleGroupName" label="电销组">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" v-if="scope.row.teleGroupId!='99999'" @click="gotoTeam(scope.row)">{{scope.row.teleGroupName}}</el-button>
                        <span v-else>合计</span>
                    </template>
                </el-table-column>
                <el-table-column prop="resNum" label="分配资源数" :formatter="format_number"></el-table-column>
                <el-table-column prop="noVisitRate" label="未跟访占比"></el-table-column>
                <el-table-column prop="halfHourRate" label="30分钟内跟访占比"></el-table-column>
                <el-table-column prop="oneHourRate" label="1小时内跟访占比"></el-table-column>
                <el-table-column prop="twoHourRate" label="2小时内跟访占比"></el-table-column>
                <el-table-column prop="threeHourRate" label="3小时内跟访占比"></el-table-column>
                <el-table-column prop="sixHourRate" label="6小时内跟访占比"></el-table-column>
                <el-table-column prop="twelveHourRate" label="12小时内跟访占比"></el-table-column>
                <el-table-column prop="twentyFourRate" label="24小时内跟访占比"></el-table-column>
                <el-table-column prop="moreRate" label="24小时外跟访占比"></el-table-column>
            </el-table>
            <table-pagination :pager="pager" @change="searchTable()"></table-pagination>
        </el-row>
    </div>
</div>
<!-- import common js-->
<div th:include="_footer_include :: #jsLib"></div>
</body>
<style>
    .el-table tr:nth-of-type(1) td:nth-of-type(n+1) {font-weight: bold;}
</style>
<script th:inline="javascript">
    var deptList=[[${deptList}]];// 事业部
    var curDeptId =[[${deptId}]]; // 当前用户事业部id
    var curOrgId = [[${teleGroupId}]]; // 当前用户电销组id
    var mainDivVM = new Vue({
        el: '#maindiv',
        data: function() {
            return {
                loading: false,
                isDisabled: false,
                isDisabledOrgId: false,
                pager:{
                    total: 0,
                    currentPage: 1,
                    pageSize: 20,
                },
                form: {
                    startTime:'',
                    endTime:'',
                    teleDeptId: '',
                    teleGroupId: '',
                    source:''
                },
                export: {
                    startTime:'',
                    endTime:'',
                    teleDeptId: '',
                    teleGroupId: '',
                    source: ''
                },
                tableData: [],
                deptList: deptList, // 电销事业部
                orgTeleArr: '', // 电销组
                busSaleArr: '', // 电销顾问
                sourceList: [{id: 1,name: '新资源'},{id: 0,name: '非新资源'}]
            }             
        },
        methods: {
            format_number(row, column){ // 千分位数字格式化
                var nStr = row[column.property];
                if('--'==nStr){
                    return nStr;
                }
                nStr += '';
                if(nStr.indexOf('.') == -1){
                    var x1 = nStr;
                    var rgx = /(\d+)(\d{3})/;
                    while (rgx.test(x1)) {
                        x1 = x1.replace(rgx, '$1' + ',' + '$2');
                    }
                    return x1;
                }else{
                    var x = nStr.split('.');
                    x1 = x[0];
                    if(!x[1]){
                        x[1] = '00';
                    }
                    x2 = x.length > 1 ? '.' + x[1] : '';
                    var rgx = /(\d+)(\d{3})/;
                    while (rgx.test(x1)) {
                        x1 = x1.replace(rgx, '$1' + ',' + '$2');
                    }
                    return x1 + x2;
                }
            },
            centerStyle(){
                return "text-align:center"
            },
            firstCurTelSale(seleVal){
                if(seleVal==null||seleVal==""){
                    return ;
                }
                this.getTelSale(seleVal);
            },
            curTelSale(seleVal){
                if(seleVal==null||seleVal==""){
                    return ;
                }
                this.form.teleSaleId = '';
                this.getTelSale(seleVal);
            },
            firstCurTelGroupSale(seleVal){
                if(seleVal==null||seleVal==""){
                    return ;
                }
                this.getTelGroupSale(seleVal);
            },
            curTelGroupSale(seleVal){
                if(seleVal==null||seleVal==""){
                    return ;
                }
                this.form.teleGroupId = '';
                // this.form.teleSaleId = '';
                this.getTelGroupSale(seleVal);
            },
            getTelGroupSale(seleVal){
                param={};
                param.parentId=seleVal;
                param.orgType=1;
                axios.post('/base/getGroupList',param).then(function (response) {
                    mainDivVM.orgTeleArr=response.data.data;
                }).catch(function (error) {
                        mainDivVM.$message({
                        message: "电销组请求失败，请稍后重试",
                        type: "error"
                    });
                });
            },
            getTelSale(seleVal){
                param={};
                param.orgId=seleVal;
                param.roleCode="DXCYGW";
                axios.post('/base/getSaleList' ,param).then(function (response) {  
                    mainDivVM.busSaleArr=response.data.data;
                }).catch(function (error) {
                        mainDivVM.$message({
                        message: "电销顾问请求失败，请稍后重试",
                        type: "error"
                    });
                });
            },
            searchTable(firstPage) { 
                var startTime = this.form.startTime;
                var endTime = this.form.endTime;
                if(endTime && startTime){
                    startTime = new Date(this.form.startTime);
                    endTime = new Date(this.form.endTime);
                    if(startTime>endTime){
                        this.$message({
                            message: '开始时间不能大于结束时间',
                            type: 'warning'
                        });
                        return;
                    }
                    var date = new Date();
                    var currentDate = moment(date).format("YYYY-MM-DD 23:59");
                    date.setMonth(date.getMonth()-6);
                    var beforeSixMonth = moment(date).format("YYYY-MM-DD 00:00");
                    var currentDateRes = new Date(currentDate);
                    var beforeSixMonthRes = new Date(beforeSixMonth);
                    if(startTime < beforeSixMonthRes || endTime > currentDateRes){
                        this.$message({
                            message: '请选择近6个月内的时间',
                            type: 'warning'
                        });
                        return;
                    }
                    startTime = this.limitTime(startTime, null);
                    endTime = this.limitTime(null, endTime);
                }else{
                    this.$message({
                        message: '请选择起止时间',
                        type: 'warning'
                    });
                    return;
                }   
    
                var param = {};
                param.startTime = this.minuteFormat(startTime);
                param.endTime = this.minuteFormat(endTime);
                param.pageSize = this.pager.pageSize;
  
                if(firstPage){
                    param.pageNum = firstPage;
                }else{
                    param.pageNum = this.pager.currentPage;
                }
                param.teleDeptId = this.form.teleDeptId;
                param.teleGroupId = this.form.teleGroupId;
                param.source = this.form.source;
                this.loading = true; 
                axios.post('/resourceVisit/queryByPage',param).then(function (response) {
                    if(!response || !response.data){
                        mainDivVM.$message({
                            message: "接口调用失败",
                            type: 'error'
                        });
                        return ;
                    }
                    var resobj= response.data;
                    if(resobj.code != '0'){
                        mainDivVM.loading = false;
                        mainDivVM.$message({
                            message: resobj.msg,
                            type: 'error'
                        });
                        return ;
                    }else if(resobj.data && resobj.data.tableData){
                        var resobjData = resobj.data;    
                        var tableData = resobjData.tableData;
                        mainDivVM.tableData = tableData.data;
                        if( tableData.data.length && resobj.data.totalData ){
                            var totalData = resobjData.totalData[0];
                            mainDivVM.tableData.unshift(totalData);
                        }
                        mainDivVM.pager.currentPage = tableData.currentPage;
                        mainDivVM.pager.total = tableData.total;
                        mainDivVM.loading = false;
                        mainDivVM.export.startTime = mainDivVM.minuteFormat(startTime);
                        mainDivVM.export.endTime = mainDivVM.minuteFormat(endTime);
                        mainDivVM.export.teleDeptId = mainDivVM.form.teleDeptId;
                        mainDivVM.export.teleGroupId = mainDivVM.form.teleGroupId;
                        mainDivVM.export.source = mainDivVM.form.source;
                    }else{
                        mainDivVM.loading = false;
                    }
                }).catch(function (error) {
                    mainDivVM.loading = false;
                    mainDivVM.$message({
                        message: "请求失败，请稍后重试",
                        type: "error"
                    });
                });
            },
            limitTime (start, end) {
                var endTime = this.minuteFormat (end);
                var startTime = this.minuteFormat (start);
                var date = new Date();
                data = this.minuteFormat (date);// 现在的时间
                data = data.slice(0,8);
                if(startTime){
                    var s = startTime.slice(0,8);
                    if(s < data){
                        this.form.startTime = moment(start).format("YYYY-MM-DD 00:00");
                        return this.form.startTime;
                    }
                    return start;
                }
                if(endTime){
                    var e = endTime.slice(0,8);
                    if(e < data){
                        this.form.endTime = moment(end).format("YYYY-MM-DD 23:59");
                        return this.form.endTime;                        
                    }
                    return end;
                }
            },
            gotoTeam(row){
                var teleGroupId = row.teleGroupId;
                var teleDeptId = this.export.teleDeptId;
                var source = this.export.source;
                var startTime = this.export.startTime;
                var endTime = this.export.endTime;
                if(teleGroupId){
                    window.location.href="/resourceVisit/managerList?teleDeptId=" + teleDeptId + "&teleGroupId="+ teleGroupId + "&source="+ source +"&startTime=" + startTime + "&endTime=" + endTime; 
                }
            },
            arraySpanMethod({ row, column, rowIndex, columnIndex }) { // 合并第一行
                if (rowIndex === 0) {
                    if (columnIndex === 0) {
                        return [0, 0];
                    } else if (columnIndex === 1) {
                        return [1, 2];
                    }
                }
            },
            indexFun (index) { // 表格序号
                return index;
            },
            minuteFormat (m) {
                if (!m) {return "";}
                var timeStr = new Date(m);
                var yearRes = timeStr.getFullYear()
                var monthRea = timeStr.getMonth()+1;
                monthRea = monthRea < 10 ?  "0" + monthRea : monthRea;
                var dateRes = timeStr.getDate();
                dateRes = dateRes < 10 ?  "0" + dateRes : dateRes;  
                var hoursRes = timeStr.getHours();
                hoursRes = hoursRes < 10 ?  "0" + hoursRes : hoursRes;  
                var minRes = timeStr.getMinutes();
                minRes = minRes < 10 ?  "0" + minRes : minRes;                
                var timeRes = "" + yearRes + monthRea + dateRes + hoursRes + minRes;
                return timeRes;
            },
            searchYesterday(){ // 昨天
               var today = new Date();
               today.setTime(today.getTime()-24*60*60*1000);
               var startTime = today.getFullYear()+"-" + (today.getMonth()+1) + "-" + today.getDate()+" 00:00";
               var endTime =today.getFullYear()+"-" + (today.getMonth()+1) + "-" + today.getDate()+" 23:59";
               this.form.startTime=startTime;
               this.form.endTime=endTime;
               this.searchTable(1);
            },
            searchWeek(){ // 近七天
                 var a = new Date();
                 var year = a.getFullYear();
                 var month = a.getMonth();
                 var date = a.getDate();
                 var b = new Date(year,month,date);
                 var c = b.valueOf()-6*24*60*60*1000;
                 var d = b.valueOf();
                 var startTime= new Date(c);
                 var endTime = new Date(d);
                 this.form.startTime=startTime.getFullYear()+"-" + (startTime.getMonth()+1) + "-" + startTime.getDate()+" 00:00";
                 this.form.endTime=endTime.getFullYear()+"-" + (endTime.getMonth()+1) + "-" + endTime.getDate()+" 23:59";
                 this.searchTable(1);
            },
            searchMonth(){ // 本月 
                var a = new Date();
                var year = a.getFullYear();
                var month = a.getMonth();
                var date = a.getDate();
                var b = new Date(year,month,date);
                var f= new Date(year,month,01);
                var c = f.valueOf();
                var startTime= new Date(c);
                this.form.startTime=startTime.getFullYear()+"-" + (startTime.getMonth()+1) + "-" + startTime.getDate()+" 00:00";
                this.form.endTime=year+"-"+(month+1)+"-"+date+" 23:59";
                this.searchTable(1);
            },
            exportRs(){
                var param = {};
                param.startTime = this.export.startTime;
                param.endTime = this.export.endTime;
                param.teleDeptId = this.export.teleDeptId;
                param.teleGroupId = this.export.teleGroupId;
                param.source = this.export.source;
                axios.post('/resourceVisit/export',param,{responseType:'blob'})
                .then(function (response) {
                    var data =  response.data;
                    var fileName = response.headers.filename;
                    saveAs(data,decodeURI(fileName));
                })
                .catch(function (error) {
                    mainDivVM.$message({
                        message: "导出失败，请稍后重试",
                        type: "error"
                    });
                });
            }
        },
        created(){ 
            var today = new Date();
            var startTime = moment(today).format("YYYY-MM-DD 00:00");
            var endTime = moment(today).format("YYYY-MM-DD 23:59");
            this.form.startTime=startTime;
            this.form.endTime=endTime; 
            if(curDeptId){
                this.isDisabled = true;
                this.form.teleDeptId = curDeptId; 
                this.firstCurTelGroupSale(curDeptId);
                if(curOrgId){
                    this.form.teleGroupId = curOrgId; 
                    this.isDisabledOrgId = true;
                    this.export.teleGroupId = this.form.teleGroupId;       
                }
                this.export.teleDeptId = this.form.teleDeptId;       
            }
            this.export.startTime = this.minuteFormat(this.form.startTime);
            this.export.endTime = this.minuteFormat(this.form.endTime);

            var localVal=localStorage.getItem('allChangePageSize')?parseInt(localStorage.getItem('allChangePageSize')):'';
            if(localVal){this.pager.pageSize = localVal;}

            this.searchTable();
        },
        mounted(){
            document.getElementById('maindiv').style.display = 'block';
        }
    })
    
</script>
</html>